package mysqlconn;

import com.alibaba.fastjson.JSON;
import org.I0Itec.zkclient.ZkClient;

import java.util.HashMap;

public class ConfigServer {
    private ZkClient zkClient = new ZkClient("192.168.8.103:2181,192.168.8.104:2181");

    public void updateConfig(String driver, String url, String user, String password) {
        if (!zkClient.exists("/mysql-connect")) {
            zkClient.createPersistent("/mysql-connect");
        }
        HashMap<String, String> config = new HashMap<String, String>();
        config.put("mysql.driver", driver);
        config.put("mysql.url", url);
        config.put("mysql.user", user);
        config.put("mysql.password", password);

        zkClient.writeData("/mysql-connect", JSON.toJSONString(config));
    }

    public static void main(String[] args) throws InterruptedException {
        MyDataSource.init();
        Thread.sleep(2000);
        ConfigServer server = new ConfigServer();

        server.updateConfig("com.mysql.jdbc.driver", "jdbc:mysql://192.168.8.108:3306/mydb", "root", "524818");
        Thread.sleep(20000);
    }
}
